package com.sany.taskmanager.ui.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.viewbinding.ViewBinding;

import com.sany.taskmanager.R;
import com.sany.taskmanager.databinding.DialogCommonBinding;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class CommonDialog<VB extends ViewBinding> extends Dialog {
    protected VB vBinding;
    protected Context mContext;
    protected String mTitle;
    protected int mWidth;
    protected int mHeight;
    protected int mTitleMarginTop;
    protected int mTitleMarginStart;

    public CommonDialog(@NonNull Context context, String title) {
        super(context, R.style.CustomDialog);
        mContext = context;
        mTitle = title;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DialogCommonBinding dialogCommonBinding = DialogCommonBinding.inflate(getLayoutInflater());
        setContentView(dialogCommonBinding.getRoot());
        setCanceledOnTouchOutside(false);
        setRect();
        WindowManager.LayoutParams params = getWindow().getAttributes();
        if (mWidth != 0) {
            params.width = mWidth;
        } else {
            params.width = 711;
        }
        if (mHeight != 0) {
            params.height = mHeight;
        } else {
            params.height = 420;
        }
        getWindow().setAttributes(params);

        Type superclass = getClass().getGenericSuperclass();
        Class<?> aClass = (Class<?>) ((ParameterizedType) superclass).getActualTypeArguments()[0];
        try {
            Method method = aClass.getDeclaredMethod("inflate", LayoutInflater.class);
            vBinding = (VB) method.invoke(null, getLayoutInflater());
            dialogCommonBinding.flCustom.addView(vBinding.getRoot());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }

        if (mTitleMarginTop != 0 && mTitleMarginStart != 0) {
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(dialogCommonBinding.tvTitle.getLayoutParams());
            layoutParams.setMargins(mTitleMarginStart, mTitleMarginTop, 0, 0);
            dialogCommonBinding.tvTitle.setLayoutParams(layoutParams);
        }
        dialogCommonBinding.tvTitle.setText(mTitle);
        dialogCommonBinding.tvCancel.setOnClickListener(v -> cancel());
        dialogCommonBinding.tvConfirm.setOnClickListener(v -> {
            if (confirm()) {
                cancel();
            }
        });
        if (showBottomButtons()) {
            dialogCommonBinding.llBottom.setVisibility(View.VISIBLE);
        } else {
            dialogCommonBinding.llBottom.setVisibility(View.GONE);
        }
        if (showOnlyCancelButton()) {
            dialogCommonBinding.tvConfirm.setVisibility(View.GONE);
        } else {
            dialogCommonBinding.tvConfirm.setVisibility(View.VISIBLE);
        }
        initData();
    }

    protected void setRect() {

    }

    protected abstract void initData();

    protected abstract boolean confirm();

    public boolean showBottomButtons() {
        return true;
    }

    public boolean showOnlyCancelButton() {
        return false;
    }

}
